package rhc.taskSchedule.command.impl;

import org.apache.commons.lang3.StringUtils;

import rhc.discribute.node.host.Host;
import rhc.taskSchedule.command.CommandExecuter;
import rhc.taskSchedule.commander.TaskScheduleCommanderExecute;
import rhc.taskSchedule.common.CommandConstants;
import rhc.taskSchedule.entity.Running;
import rhc.taskSchedule.entity.RunningCode;
import rhc.taskSchedule.entity.RunningType;
import rhc.taskSchedule.exception.RunningTypeException;
import rhc.taskSchedule.taskDistribute.TaskDistribute;
import rhc.taskSchedule.taskSchedule.schedule.TaskScheduleManage;
import rhc.taskSchedule.taskSchedule.task.TaskExecuteManage;

import com.alibaba.fastjson.JSONObject;

/**命令执行器实现
 * @author rhc
 *
 */
public class CommandExecuterImpl implements CommandExecuter {

	private TaskScheduleCommanderExecute taskScheduleCommanderExecute;
	
	public CommandExecuterImpl(TaskScheduleCommanderExecute taskScheduleCommanderExecute){
		this.taskScheduleCommanderExecute=taskScheduleCommanderExecute;
	}
	
	@Override
	public void executeCommand(String command, Host host) throws Exception {
		JSONObject commandJson=JSONObject.parseObject(command);
		
		String commandCode=commandJson.getString(CommandConstants.COMMAND_CODE);
		if(StringUtils.isNoneEmpty(commandCode) && commandCode.equals(CommandConstants.RUNNING_COMMAND_CODE)){
			JSONObject commandData=commandJson.getJSONObject(CommandConstants.COMMAND_DATA);
			
			String runningId=commandData.getString(CommandConstants.RUNNING_ID);
			int runningCode=commandData.getIntValue(CommandConstants.RUNNING_CODE);
			
			if(runningCode == RunningCode.RUN.code){
				this.executeRunCommand(runningId, host);
			}else if(runningCode == RunningCode.STOP.code){
				this.executeStopCommand(runningId, host);
			}else if(runningCode == RunningCode.PAUSE.code){
				this.executePauseCommand(runningId, host);
			}else if(runningCode == RunningCode.REGAIN.code){
				this.executeRegainCommand(runningId, host);
			}
		}
		
	}

	@Override
	public void executeRunCommand(String runningId, Host host) throws Exception {
		String runningType=this.getRunningType(runningId);
		
		if(runningType.equals(RunningType.SCHEDULE.type)){
			TaskScheduleManage taskScheduleManage=taskScheduleCommanderExecute.getTaskScheduleManage();
			taskScheduleManage.run(runningId);
		}else if(runningType.equals(RunningType.TASK.type)){
			TaskExecuteManage taskExecuteManage=taskScheduleCommanderExecute.getTaskExecuteManage();
			taskExecuteManage.run(runningId);
		}else{
			throw new RunningTypeException("运行 {} 未知运行类型 {}",runningId,runningType);
		}
	}

	@Override
	public void executeStopCommand(String runningId, Host host) throws Exception {
		String runningType=this.getRunningType(runningId);
		
		if(runningType.equals(RunningType.SCHEDULE.type)){
			TaskScheduleManage taskScheduleManage=taskScheduleCommanderExecute.getTaskScheduleManage();
			taskScheduleManage.stop(runningId);
		}else if(runningType.equals(RunningType.TASK.type)){
			TaskExecuteManage taskExecuteManage=taskScheduleCommanderExecute.getTaskExecuteManage();
			taskExecuteManage.stop(runningId);
		}else{
			throw new RunningTypeException("运行 {} 未知运行类型 {}",runningId,runningType);
		}
	}

	@Override
	public void executePauseCommand(String runningId, Host host) throws Exception {
		String runningType=this.getRunningType(runningId);
		
		if(runningType.equals(RunningType.SCHEDULE.type)){
			TaskScheduleManage taskScheduleManage=taskScheduleCommanderExecute.getTaskScheduleManage();
			taskScheduleManage.pause(runningId);
		}else if(runningType.equals(RunningType.TASK.type)){
			TaskExecuteManage taskExecuteManage=taskScheduleCommanderExecute.getTaskExecuteManage();
			taskExecuteManage.pause(runningId);
		}else{
			throw new RunningTypeException("运行 {} 未知运行类型 {}",runningId,runningType);
		}
	}

	@Override
	public void executeRegainCommand(String runningId, Host host) throws Exception {
		String runningType=this.getRunningType(runningId);
		
		if(runningType.equals(RunningType.SCHEDULE.type)){
			TaskScheduleManage taskScheduleManage=taskScheduleCommanderExecute.getTaskScheduleManage();
			taskScheduleManage.regain(runningId);
		}else if(runningType.equals(RunningType.TASK.type)){
			TaskExecuteManage taskExecuteManage=taskScheduleCommanderExecute.getTaskExecuteManage();
			taskExecuteManage.regain(runningId);
		}else{
			throw new RunningTypeException("运行 {} 未知运行类型 {}",runningId,runningType);
		}
	}
	
	/**获取一个运行id的运行类型
	 * @param runningId
	 * @return
	 * @throws Exception
	 */
	private String getRunningType(String runningId) throws Exception{
		TaskDistribute taskDistribute=taskScheduleCommanderExecute.getTaskDistribute();
		Running running=taskDistribute.getRunning(runningId);
		
		String runningType=running.getRunningType();
		
		return runningType;
	}

}
